﻿using System;
using System.Drawing;
using System.Windows.Forms;
using System.IO;
using System.Collections.Generic;

namespace DiskGarden
{
	public partial class FrmAddVolume : Form
	{
		private List<DriveInfo> drives;
		public string Path
		{
			get
			{
				if (rbFolder.Checked) return tFolder.Text;
				else return drives[cbDrives.SelectedIndex].Name;
			}
		}

		public string VolumeName
		{
			get
			{
				if (rbFolder.Checked) return tFolder.Text;
				else return drives[cbDrives.SelectedIndex].VolumeLabel;
			}
		}

		public FrmAddVolume()
		{
			InitializeComponent();
			Localize();
		}

		protected override void OnLoad(EventArgs e)
		{
			base.OnLoad(e);
			// wypisuje dostępne napędy
			DriveInfo[] allDrives = DriveInfo.GetDrives();
			drives = new List<DriveInfo>(allDrives.Length);
			foreach (DriveInfo drv in allDrives)
			{
				if (drv.Name.StartsWith("A:\\")) continue;
				if (drv.IsReady)
				{
					string s = string.Format("{0} ({1})  [{2}]", drv.Name, drv.VolumeLabel, drv.DriveFormat);
					cbDrives.Items.Add(s);
					drives.Add(drv);
				}
			}
			RadioButtonChanged();
		}

		private void Localize()
		{
			Locale locale = Locale.GetInstance();
			this.Text = locale.AddDisk;
			btnOK.Text = locale.OK;
			btnCancel.Text = locale.Cancel;
			rbDrive.Text = locale.SelectDrive;
			rbFolder.Text = locale.SelectFolder;
		}

		// włącza lub wyłącza komponenty zaleznie od zaznaczonej opcji
		private void RadioButtonChanged()
		{
			cbDrives.Enabled = rbDrive.Checked;
			tFolder.Enabled = rbFolder.Checked;
			bFolder.Enabled = rbFolder.Checked;
		}

		private void rb_CheckedChanged(object sender, EventArgs e)
		{
			RadioButtonChanged();
		}

		// okno wyboru katalogu
		private void bFolder_Click(object sender, EventArgs e)
		{
			if (folderDialog.ShowDialog() == DialogResult.OK)
			{
				tFolder.Text = folderDialog.SelectedPath;
			}
		}

		private void FrmAddVolume_FormClosing(object sender, FormClosingEventArgs e)
		{
			if (DialogResult == DialogResult.OK)
			{
				if (rbFolder.Checked && !Directory.Exists(tFolder.Text))
				{
					MessageBox.Show(Locale.I.FolderDoesntExist);
					e.Cancel = true;
				}
				else if (rbDrive.Checked && cbDrives.SelectedIndex == -1)
				{
					MessageBox.Show(Locale.I.YouDontChooseDrive);
					e.Cancel = true;
				}
			}
		}

	}
}
